package com.havilahshawn.finaltest22ndsesforad;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.havilahshawn.finaltest22ndsesforad.DB.StuContract;
import com.havilahshawn.finaltest22ndsesforad.DB.StudentDbOpenHelper;

/**
 * Created by My Family on 2018/6/28.
 */

public class StuProvider extends ContentProvider {
    private StudentDbOpenHelper mStudentDbOpenHelper;
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int STUS = 100;
    private static final int STU_ID = 101;

    static {
        sUriMatcher.addURI(StuContract.CONTENT_AUTHORITY,StuContract.PATH,STUS);
        sUriMatcher.addURI(StuContract.CONTENT_AUTHORITY,StuContract.PATH+"/#",STU_ID);
    }

    @Override
    public boolean onCreate() {
        mStudentDbOpenHelper = new StudentDbOpenHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        SQLiteDatabase db = mStudentDbOpenHelper.getReadableDatabase();
        Cursor cursor = null;
        int match = sUriMatcher.match(uri);
        switch (match){
            case STUS:
                cursor = db.query(StuContract.StuEntry.TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case STU_ID:
//                selection = StuContract.StuEntry._ID + "=?";
                break;
            default:
                throw new IllegalArgumentException("查询不了未知的URI " + uri);
        }
        cursor.setNotificationUri(getContext().getContentResolver(),uri);

        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {


        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }
}
